﻿

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Net.Security;
using System.Web;

namespace LingDingSoft.Http
{
    public class HttpRequestHelper
    {
        public string SendRequest(string url, ICollection<KeyValuePair<string, string>> requestParams)
        {
            Encoding coding = AppConfig.GetEncoding();
            return SendRequest(url, new MultipartBodyWriter(requestParams, coding), coding);
        }
        public string SendRequest(string url, ICollection<KeyValuePair<string, string>> requestParams, Encoding encoding)
        {
            return SendRequest(url, new MultipartBodyWriter(requestParams, encoding), encoding);
        }
        public string SendRequest(string url, ICollection<KeyValuePair<string, string>> requestParams, ICollection<KeyValuePair<string, string>> header)
        {
            Encoding coding = AppConfig.GetEncoding();
            return SendRequest(url, new MultipartBodyWriter(requestParams, coding), coding, header);
        }
        public string SendRequest(string url, IBodyWriter bodyWriter, ICollection<KeyValuePair<string, string>> header = null)
        {
            Encoding coding = AppConfig.GetEncoding();
            return SendRequest(url, bodyWriter, coding, header);
        }

        public string SendRequest(string url, IBodyWriter bodyWriter, Encoding encoding, ICollection<KeyValuePair<string, string>> DictHeader = null)
        {
            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Accept = "*/*";
            request.KeepAlive = true;
            request.Timeout = 10000;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)";

            if (bodyWriter != null)
            {
                bodyWriter.Writer(request);

            }
            if (DictHeader != null)
            {
                foreach (var item in DictHeader)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }
            var response = request.GetResponse();
            using (var s = response.GetResponseStream())
            {
                var reader = new StreamReader(s, encoding);
                return reader.ReadToEnd();
            }

        }
    }
}

